package com.czl.module_main.fragment

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.PhoneUtils
import com.bumptech.glide.Glide
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.UserBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.base.util.PermissionUtil
import com.czl.module_main.BR
import com.czl.module_main.R
import com.czl.module_main.adapter.CommunityAdapter
import com.czl.module_main.adapter.NoticeAdapter
import com.czl.module_main.databinding.FragmentHomeBinding
import com.czl.module_main.viewmodel.HomeViewModel
import com.gyf.immersionbar.ImmersionBar
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

@Route(path = AppConstants.Router.Main.F_HOME)
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    private var communityAdapter: CommunityAdapter? = null
    private var noticeAdapter: NoticeAdapter? = null
    private var areaList: List<UserBean.Data>? = null

    override fun initContentView(): Int {
        return R.layout.fragment_home
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun isImmersionBarEnabled(): Boolean {
        return true
    }

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .fitsSystemWindows(true)
            .statusBarDarkFont(true)
            .statusBarColor(com.czl.base.R.color.color_toolbar)
            .init()
    }

    override fun initData() {
        areaList = viewModel.model.getAreaList()
        communityAdapter = CommunityAdapter(this)
        binding.rvCommunity.apply {
            adapter = communityAdapter
        }

        noticeAdapter = NoticeAdapter(this)
        binding.rvNotice.apply {
            adapter = noticeAdapter
        }

        viewModel.getCommunityData()
        viewModel.getNoticeData()
        viewModel.queryHouseKeeperList()

    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initViewObservable() {

        var bannerSrc = ArrayList<Int?>().apply {
            add(R.mipmap.icon_banner1)
            add(R.mipmap.icon_banner2)
            add(R.mipmap.icon_banner3)
        }
        binding.bannerHome.addBannerLifecycleObserver(this)
            .setAdapter(object : BannerImageAdapter<Int?>(bannerSrc) {
                override fun onBindView(
                    holder: BannerImageHolder?,
                    data: Int?,
                    position: Int,
                    size: Int
                ) {
                    Glide.with(holder!!.itemView).load(data).into(holder.imageView)
                }
            }).setIndicator(CircleIndicator(requireContext()))
        viewModel.uc.switchAreaEvent.observe(this, {
            areaList?.let {
                DialogHelper.showBottomListDialog(
                    requireContext(),
                    Stream.of(it).map { t -> t.areaName }.withoutNulls()
                        .collect(Collectors.toList()) as ArrayList<String>
                ) { position, text ->
                    viewModel.areaName.set(text)
                    viewModel.model.saveAreaId(areaList!![position].areaId)
                    viewModel.model.saveAreaName(text)
                    viewModel.getCommunityData()
                    viewModel.getNoticeData()
                    viewModel.queryHouseKeeperList()
                }
            }
        })

        viewModel.toastDoingEvnent.observe(this, {
            showNormalToast("正在开发中")
        })

        viewModel.uc.deleteResultEvent.observe(this, {
            viewModel.getMyQueryHistory()
        })

        LiveBusCenter.observeRefreshHisCompleteEvent(this, {
            viewModel.getMyQueryHistory()
        })
        viewModel.uc.loadActivityEvent.observe(this, {
            communityAdapter!!.setList(it.records)
        })
        viewModel.uc.loadNoticeEvent.observe(this, {
            noticeAdapter!!.setList(it.records)
        })
        viewModel.uc.homeRefreshEvent.observe(this,{
            binding.smartCommon.finishRefresh()
        })
        viewModel.uc.callEvent.observe(this, {
            PermissionUtil.reqCall(null, this) { allGranted, _, _ ->
                if (allGranted) {
                    if (ActivityCompat.checkSelfPermission(
                            requireContext(),
                            Manifest.permission.CALL_PHONE
                        ) == PackageManager.PERMISSION_GRANTED
                    ) {
                        PhoneUtils.call(it)
                    }

                }
            }
        })
        LiveBusCenter.observeCommunityRefreshEvent(this,{
            viewModel.getCommunityData()
        })
        LiveBusCenter.observeBulletinRefreshEvent(this,{
            viewModel.getNoticeData()
        })
        LiveBusCenter.observeHouseKeeperRefreshEvent(this,{
            viewModel.queryHouseKeeperList()
        })
    }

    override fun useBaseLayout(): Boolean {
        return false
    }
}